//
//  TJHomeDetailController.m
//  Teleport
//
//  Created by hc-101 on 2020/6/16.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJHomeDetailController.h"
#import "TJHomeDetailHeaderView.h"
#import "TJHomeDetaiCell.h"
#import "TJTransferDetailController.h"
#import "TJOrderDetailControntroller.h"
#import "TJWithdrawalController.h"
#import "TJBuySellNeedAuthController.h"

@interface TJHomeDetailController ()

@property (strong, nonatomic) NSMutableArray *datas;
@property (assign, nonatomic) int pageIndex;
@property (copy, nonatomic) NSString *type;
//@property (strong, nonatomic) NSDictionary *authDict;


@property (copy, nonatomic) NSString *coinNo;

@end

static NSString * const homeDetaiCell = @"homeDetaiCell";

@implementation TJHomeDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _pageIndex = 1;
    _datas = @[].mutableCopy;
    //货币编号,1btc 2usdt
    _coinNo = _dict[@"coinNo"];
    
    self.navigationBar.alpha = 0;
    self.navigationBar.lineView.hidden = YES;
    [self.navigationBar backBarButtonWhiteColor];
    self.navigationBar.titleTextColor = UIColor.whiteColor;
    
    
    TJHomeDetailHeaderView *headerView = [[TJHomeDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 196 + StatusBarHeight - 20)];
    headerView.dict = _dict;
    //筛选
    [headerView.screeningButton addTarget:self action:@selector(didClickScreeningButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:headerView];
    
    self.view.backgroundColor = UIColor.whiteColor;
    self.tableView.backgroundColor = UIColor.whiteColor;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = 90;
    [self.tableView registerClass:TJHomeDetaiCell.self forCellReuseIdentifier:homeDetaiCell];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_bottom);
        make.left.right.offset(0);
        make.bottom.offset(-48 - TabbarSafeBottomMargin);
    }];
    
    
    
    UIView *bottomView = [UIView tj_addToView:self.view];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-TabbarSafeBottomMargin);
        make.height.offset(48);
        make.left.right.offset(0);
    }];
    
    UIView *lineView = [UIView tj_addToView:bottomView];
    lineView.backgroundColor = UIColor.tj_separatorColor;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(0);
        make.height.offset(0.7);
    }];
    
    UIButton *topUpButton = [UIButton buttonWithSuperView:bottomView font:UIFont_Px(12) color:UIColor_Hex(0x262626) title:@"充币".localizedString];
    [topUpButton setImage:[UIImage imageNamed:@"header_tip_icon"] forState:UIControlStateNormal];
    [topUpButton setImage:[UIImage imageNamed:@"header_tip_icon"] forState:UIControlStateHighlighted];
    [topUpButton addTarget:self action:@selector(didClickTopUpButton) forControlEvents:UIControlEventTouchUpInside];
    [topUpButton setImagePosition:LXMImagePositionTop spacing:3];
    [topUpButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(50);
        make.left.offset(80);
        make.top.bottom.offset(0);
    }];
    
    
    UIButton *withdrawalButton = [UIButton buttonWithSuperView:bottomView font:UIFont_Px(12) color:UIColor_Hex(0x262626) title:@"提币".localizedString];
    [withdrawalButton addTarget:self action:@selector(didClickWithdrawalButton) forControlEvents:UIControlEventTouchUpInside];
    [withdrawalButton setImage:[UIImage imageNamed:@"header_tip_icon"] forState:UIControlStateNormal];
    [withdrawalButton setImage:[UIImage imageNamed:@"header_tip_icon"] forState:UIControlStateHighlighted];
    [withdrawalButton setImagePosition:LXMImagePositionTop spacing:3];
    [withdrawalButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(70);
        make.right.offset(-80);
        make.top.bottom.offset(0);
    }];
    /**数据加载 */
   // [self setUpData];
    

    
}

/**充币点击 **/
- (void)didClickTopUpButton {
    [self loadAuthDataWithType:1];
}
/**提币点击 **/
- (void)didClickWithdrawalButton {
    [self loadAuthDataWithType:2];
}

- (void)loadAuthDataWithType:(NSInteger)type {
    weakify(self)
    [TJNetworking.shareNetworking updateUserInfoAndLoading:YES success:^(TJUserInfo * _Nonnull userInfo) {
        
        if (userInfo.bankCard.replacingEmptyString.length &&  userInfo.cardNo.replacingEmptyString.length) {
            if (type == 1) {//充币
                [weakself.navigationController pushViewControllerWithName:@"TJTopUpController" title:nil animated:YES];
            } else {//提币
                TJWithdrawalController *withdrawalController = [[TJWithdrawalController alloc] init];
                ////货币编号,1btc 2usdt
//                withdrawalController.coinNo = @"1";
                [weakself.navigationController pushViewController:withdrawalController animated:YES];
            }
        }else{
            TJBuySellNeedAuthController *buySellNeedAuthController = [[TJBuySellNeedAuthController alloc] init];
            buySellNeedAuthController.title = (type == 1) ? @"充币需要完成以下认证：".localizedString :  @"提币需要完成以下认证：".localizedString;
            [weakself presentViewController:buySellNeedAuthController animated:NO completion:nil];
        }
    }];
 
    
    
}

/**筛选点击 **/
- (void)didClickScreeningButton {
    
    TJUIAlertController *alert = [[TJUIAlertController alloc] initWithTitle:@"" message:@"" preferredStyle:QMUIAlertControllerStyleActionSheet];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:@"全部".localizedString style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        self.type = @"-1";
        [self loadNewData];
    }]];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:@"转入".localizedString style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        self.type = @"2";
        [self loadNewData];
    }]];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:@"转出".localizedString style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        self.type = @"3";
        [self loadNewData];
    }]];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:@"充币".localizedString style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        self.type = @"1";
        [self loadNewData];
    }]];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:@"提币".localizedString style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        self.type = @"0";
        [self loadNewData];
    }]];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:@"购买".localizedString style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        self.type = @"4";
        [self loadNewData];
    }]];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:@"出售".localizedString style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        self.type = @"5";
        [self loadNewData];
    }]];
    
    
    
    [alert addAction:[QMUIAlertAction actionWithTitle:@"系统加币".localizedString style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        self.type = @"6";
        [self loadNewData];
    }]];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:@"系统减币".localizedString style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        self.type = @"7";
        [self loadMoreData];
    }]];
    
    [alert addAction:[QMUIAlertAction actionWithTitle:@"取消".localizedString style:QMUIAlertActionStyleCancel handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) {
        
    }]];
    
    [alert showWithAnimated:YES];
    
}


#pragma mark ---------------tableViewDetegatel------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJHomeDetaiCell *cell = [tableView dequeueReusableCellWithIdentifier:homeDetaiCell forIndexPath:indexPath];
    cell.dict = self.datas[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    NSDictionary *dict = self.datas[indexPath.row];
    if (!dict) return;
    
    int type = [dict[@"type"] intValue];
    
    if (type == 4 || type == 5) {
        TJOrderDetailControntroller *orderDetailControntroller = [[TJOrderDetailControntroller alloc] init];
        orderDetailControntroller.orderId = dict[@"orderNo"];
        [self.navigationController pushViewController:orderDetailControntroller animated:YES];
        return;
    }
    
    TJTransferDetailController *transferDetailController = [[TJTransferDetailController alloc] init];
    transferDetailController.dict = dict;
    transferDetailController.coinNo = _coinNo;
    [self.navigationController pushViewController:transferDetailController animated:YES];
    
    
}

- (void)setUpData{
    [self loadNewData];
    //    self.tableView.mj_header = [TJRefreshHeader headerWithRefreshingBlock:^{
    //        [self loadNewData];
    //    }];
    
    //    self.tableView.mj_footer = [TJRefreshFooter footerWithRefreshingBlock:^{
    //        [self loadMoreData];
    //    }];
    self.tableView.mj_header = self.refreshHeader;
    self.tableView.mj_footer = self.refreshFooter;
    //    self.tableView.mj_footer.hidden = YES;
}


- (void)loadNewData {
    
    _pageIndex = 1;
    
    NSMutableDictionary *parameters = @{
        @"pageSize": @(10),
        @"coinNo": _coinNo,
        @"pageNum": @(_pageIndex),
        @"token": TJUserInfo.readInfo.token
    }.mutableCopy;
    
    if (_type.intValue >= 0) {
        [parameters setValue:_type forKey:@"type"];
    }
    
    [TJNetworking.shareNetworking POST:@"tradeInfo/getTradeList" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        
        self.displayEmpty = YES;
        
        NSArray *dataArr = data[@"list"];
        if (dataArr.count) {
            self.datas = dataArr.mutableCopy;
        } else {
            [self.datas removeAllObjects];
        }
        
        //        if (![data[@"page"][@"hasNextPage"] boolValue]) {//没有更多数据了
        //            [self.refreshFooter endRefreshingWithNoMoreData];
        //        }
        [self footerViewIsHidden:[data[@"page"][@"hasNextPage"] boolValue]];
        
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        
    } failure:^(NSError * _Nonnull error) {
        self.displayEmpty = YES;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    }];
    
}


- (void)loadMoreData {
    _pageIndex++;
    NSMutableDictionary *parameters = @{
        @"pageSize": @(10),
        @"coinNo": _coinNo,
        @"pageNum": @(_pageIndex),
        @"token": TJUserInfo.readInfo.token
    }.mutableCopy;
    
    if (_type.intValue >= 0) {
        [parameters setValue:_type forKey:@"type"];
    }
    
    [TJNetworking.shareNetworking POST:@"tradeInfo/getTradeList" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        
        self.displayEmpty = YES;
        
        NSMutableArray *dataArr = data[@"list"];
        if (dataArr.count) {
            [self.datas addObjectsFromArray:dataArr];
        }
        
        //        if (![data[@"page"][@"hasNextPage"] boolValue]) {//没有更多数据了
        //           // [self.refreshFooter endRefreshingWithNoMoreData];
        //            self.tableView.mj_footer.hidden = YES;
        //        }
        [self footerViewIsHidden:[data[@"page"][@"hasNextPage"] boolValue]];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError * _Nonnull error) {
        self.displayEmpty = YES;
        
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        
    }];
    
}
- (void)footerViewIsHidden:(BOOL)isHidden {
    if (isHidden) {
        self.tableView.mj_footer.hidden = NO;
    }else{
        self.tableView.mj_footer.hidden = YES;
    }
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}


@end
